<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use clt\Leftnav;
use think\Loader;
class Book extends Common
{ 
    private $wqs_zuser;
    public function __construct()
    {
        parent::__construct();
        $this -> wqs_zuser = db('wqs_zuser');
    }

    /**
     * 通讯录展示
     */
    public function index()
    {
        if(request()->isPost())
        {
            $key  = input('post.key', '');
            $page = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list = $this ->wqs_zuser
                  //-> where(['status'=>['egt',0]])
                  -> where('name','like',"%".$key."%")
                 -> order('id desc')
                 // -> field('id,name,tel,party_job,is_party,openid')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 -> toArray();
            $info = $list['data'];

            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$info,'count'=>$list['total'],'rel'=>1];
            
        }
        return view();
    }

    /**
     * 通讯录添加
     */
    public function add()
    {
        if (request() -> isPost())
        {
            $data = input('param.');
            if ($data['is_test'] == 0) {
                $data['classify_id'] = $data['cate1_id'];
                $data['dept_id']     = $data['dept1_id'];
                $data['job']         = $data['job1'];
                // $data['class']       = $data['class1'];
                // $data['open']        = $data['open1'];
            } else if ($data['is_test'] == 1) {
                $data['job']         = $data['job2'];
                $data['classify_id'] = $data['cate2_id'];
                $data['dept_id']     = $data['dept2_id'];   
                $data['class']       = $data['class2'];
                $data['open']        = $data['open2'];             
            } else if ($data['is_test'] == 2) {
                // $data['classify_id'] = $data['cate3_id'];
                // $data['dept_id']     = $data['dept3_id'];
                $data['job']         = $data['job3'];
                // $data['class']       = $data['class3'];
                // $data['open']        = $data['open3'];
            }else if ($data['is_test'] == 4) {
                $data['job']         = $data['job4'];
                $data['classify_id'] = $data['cate4_id'];
                $data['dept_id']     = $data['dept4_id'];   
                // $data['class']       = $data['class4'];
                $data['open']        = $data['open4'];  
            }   

            $info = $this ->wqs_zuser->insert($data);
            if ($info) {
                $result = ['code'=>1, 'msg'=>'添加成功! ', 'url'=>url('index')];
            } else {
                $result = ['code'=>0, 'msg'=>'添加失败! ', 'url'=>url('add')];
            }
            return $result;
        }
        $classify=db("wqs_classify")->select();
        $dept=db("wqs_dept")->select();

            $nav = new Leftnav();
            $party=db('party')->select();
            $party = $nav->menu($party);  

        $classorg=db('wqs_classorg')->where(array('open'=>1))->select();
        $this->assign('party',$party);
        $this->assign('classorg',$classorg); //组织关系单位


        $this->assign('classify',$classify);
        $this->assign('dept',$dept);
        return view();
    }

   
    public function doCate()
    {
        if (request() -> isPost())
        {
            $id   = input('param.id');
            $data = db('wqs_classify c')
                 -> join('wqs_dept d', 'd.classify_id=c.id', 'left')
                 -> field('d.*')
                 -> where(['c.id'=>$id])
                 -> select();
            return $data;
        }
    }



    public function edit()
    {
        if (request() -> isPost())
        {
            $data = input('param.');

            if ($data['is_test'] == 0) {
                $data['classify_id'] = $data['cate1_id'];
                $data['dept_id']     = $data['dept1_id'];
                $data['job']         = $data['job1'];
                // $data['class']       = $data['class1'];
                // $data['open']        = $data['open1'];
            } else if ($data['is_test'] == 1) {
                $data['job']         = $data['job2'];
                $data['classify_id'] = $data['cate2_id'];
                $data['dept_id']     = $data['dept2_id'];   
                $data['class']       = $data['class2'];
                $data['open']        = $data['open2'];             
            } else if ($data['is_test'] == 2) {
                // $data['classify_id'] = $data['cate3_id'];
                // $data['dept_id']     = $data['dept3_id'];
                $data['job']         = $data['job3'];
                // $data['class']       = $data['class3'];
                // $data['open']        = $data['open3'];
            }else if ($data['is_test'] == 4) {
                $data['job']         = $data['job4'];
                $data['classify_id'] = $data['cate4_id'];
                $data['dept_id']     = $data['dept4_id'];   
                // $data['class']       = $data['class4'];
                $data['open']        = $data['open4'];  
            }   

            $info = $this -> wqs_zuser->update($data);
            if ($info !== false)
            {
                $result = ['code'=>1, 'msg'=>'修改成功! ', 'url'=>url('index')];
            } else 
            {
                $result = ['code'=>0, 'msg'=>'修改失败! ', 'url'=>url('edit')];
            }
            return $result;
        }



        $id   = input('param.id');
        $data = $this ->wqs_zuser -> find($id);

        if ($data['is_test'] == 0) {
                $data['cate1_id']   =  $data['classify_id'];
                $data['dept1_id']   =  $data['dept_id'];
                $data['job1']       =  $data['job'];
            } else if ($data['is_test'] == 1) {
                $data['job2']       = $data['job'];
                $data['cate2_id']   = $data['classify_id'];
                $data['dept2_id']   = $data['dept_id'];
                $data['class2']     = $data['class'];
                $data['open2']      = $data['open'];
            } else if ($data['is_test'] == 2) {
                $data['job3']         = $data['job'];
            }else if ($data['is_test'] == 4) {
                $data['job4']       = $data['job'];
                $data['cate4_id']   = $data['classify_id'];
                $data['dept4_id']   = $data['dept_id'];
                // $data['class4']     = $data['class'];
                $data['open4']      = $data['open'];  
            }

        $party_info = db('party') -> find($data['party_id']);
        $this->assign('party_info',$party_info);

        $classify=db("wqs_classify")->select();
        $dept=db("wqs_dept")->select();

        $id1 = db("wqs_zuser")->where(['id'=>$id])->value('classify_id');
        $dept = db('wqs_classify c')
                 -> join('wqs_dept d', 'd.classify_id=c.id', 'left')
                 -> field('d.*')
                 -> where(['c.id'=>$id1])
                 -> select();

        $this->assign('classify',$classify);
        $this->assign('dept',$dept);      

              
        $dept=db("wqs_dept")->select();

        $nav = new Leftnav();
        $party=db('party')->select();
        $party = $nav->menu($party);  

        $classorg=db('wqs_classorg')->where(array('open'=>1))->select();
        $this->assign('party',$party);
        $this->assign('classorg',$classorg); //组织关系单位        
       

        $this -> assign('list', $data);
        return view();
    }



    public function select()
    {
      $nav = new Leftnav();

      $party=db('party')->field('id,title name,pid')->select();
      $party = $nav->tree($party); 
      // print_r($party);  
      $this->assign('party',json_encode($party));       
      return view();
    }




    /**
     * 通讯录解除绑定
     */
    public function unbind()
    {
        $id = input('param.id');
        $data['id'] = $id;
        $data['openid'] = '';
        $data['nickname'] = '';
        $data['headimgurl'] = '';
        $info = $this -> wqs_zuser->update($data);
        if ($info !== false)
        {
            $result = ['code'=>1, 'msg'=>'解除绑定成功! ', 'url'=>url('index')];
        } else 
        {
            $result = ['code'=>0, 'msg'=>'解除绑定失败! ', 'url'=>url('index')];
        }
        return $result;
    }


    /**
     * 通讯录状态
     */
    public function status()
    {
        $id=input('post.id');
        $status=$this -> wqs_zuser->where(array('id'=>$id))->value('status');
        if($status==1)
        {
            $data['status'] = 0;
            $this -> wqs_zuser->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 0;
            $result['status']  = 1;
        }else
        {
            $data['status'] = 1;
            $this -> wqs_zuser->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 1;
            $result['status'] = 1;
        }
        return $result;
    }

    /**
     * 通讯录排序
     */
    public function ruleOrder()
    {
        $data   = input('post.');
        if($this -> wqs_zuser->update($data)!==false)
        {
            return $result = ['code'=>1,'msg'=>'排序更新成功!','url'=>url('index')];
        }else
        {
            return $result = ['code'=>0,'msg'=>'排序更新失败!'];
        }
    }


    /**
     * 通讯录删除
     */
    public function del()
    {
        $id    = input('param.id');
        $info  = $this -> wqs_zuser -> delete($id);
        if ($info) {
            return $result = ['code'=>1,'msg'=>'删除成功!'];
        } else {
            return $result = ['code'=>0,'msg'=>'删除失败!'];
        }
    }

    /**
     * 导入excel
     */

    public function impExcel()
    {
        if (!empty($_FILES['import']['name'])) {
            $file = request()->file('import');
            if ($file) {
                $info = $file->validate(['ext'=>['xlsx','xls']])->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $list = $info -> getSaveName();
                } else {
                    $this->error('上传文件的后缀必须为 .xls或者 .xlsx');
                }
            }
            
        Loader::import('PHPExcel.PHPExcel', EXTEND_PATH);
            $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS .$list;
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');

            // $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
            $objPHPExcel = $objReader->load($file_name,$encode='utf-8');

            $sheet = $objPHPExcel->getSheet(0);

            $highestRow = $sheet->getHighestRow(); // 取得总行数

            // echo $highestRow;die();

            $highestColumn = $sheet->getHighestColumn(); // 取得总列数

            for($i=2;$i<=$highestRow;$i++)
            {   
                $data['name']       = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();  
                $data['dept_id']    = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                $data['tel']       = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                $data['class'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                $data['sort']  = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
                $data['open'] = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                $data['openid'] = $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
                $data['job'] = $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
                $data['is_test'] = $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
                $data['party_id'] = $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
                $data['sex'] = $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
                $data['party_time'] = $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
                $data['party_job'] = $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
                $data['is_party'] = $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
                $data['is_d_leader'] = $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
                $data['is_p_leader'] = $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
                $data['average'] = $objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
                $data['average_zuser'] = $objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
                $data['nickname'] = $objPHPExcel->getActiveSheet()->getCell("S".$i)->getValue();
                $data['headimgurl'] = $objPHPExcel->getActiveSheet()->getCell("T".$i)->getValue();
                $data['deptCheck'] = $objPHPExcel->getActiveSheet()->getCell("U".$i)->getValue();
                $data['leaderCheck'] = $objPHPExcel->getActiveSheet()->getCell("V".$i)->getValue();
                $data['p_jop'] = $objPHPExcel->getActiveSheet()->getCell("W".$i)->getValue();
                $data['classify_id'] = $objPHPExcel->getActiveSheet()->getCell("X".$i)->getValue();
                $data['card_id'] = $objPHPExcel->getActiveSheet()->getCell("Y".$i)->getValue();
                $data['org_unit'] = $objPHPExcel->getActiveSheet()->getCell("Z".$i)->getValue();
                $data['place'] = $objPHPExcel->getActiveSheet()->getCell("AA".$i)->getValue();
                $data['property'] = $objPHPExcel->getActiveSheet()->getCell("AB".$i)->getValue();
                $data['is_flow'] = $objPHPExcel->getActiveSheet()->getCell("AC".$i)->getValue();
                db('wqs_zuser')->insert($data);
            } 

            $this->success('导入成功！');

        } else {
            $this->error("请选择上传的文件");

        } 
    }
}
